# 23/100 链表-反转链表
# leetcode第206题: https://leetcode.cn/problems/reverse-linked-list/description/?envType=study-plan-v2&envId=top-100-liked
# Date: 2024/12/7
from typing import Optional
from leetcode.test import test_function as tf
from leetcode.bds import ListNode, ListConvert


def reverseList_iter(head: Optional[ListNode]) -> Optional[ListNode]:
    left = None
    curr = head
    while curr:
        right = curr.next
        curr.next = left
        left = curr
        curr = right
    return ListConvert.link_to_list(left)


if __name__ == '__main__':
    head = ListConvert.list_to_link([1, 2, 3, 4, 5])
    head1 = ListConvert.list_to_link([1, 2])
    head2 = ListConvert.list_to_link([1])

    inp = [{"head": head}, {"head": head1}, {"head": head2}, ]
    out = [[5, 4, 3, 2, 1], [2, 1], [1]]
    tf(reverseList_iter, inp, out)
